*Purpose: Post-Matching Estimation with Difference-in-Difference
** Robustness checks

clear

**0. Load data
use "$data\MASTER_MATCHED_R2.dta", clear
rename cell_uniqueID cellID

**1. Define globals 
local invarcontrols elevation_km slope_100deg NearCity_dist_100km NearCity_population i.LandSurface_code i.Bioclimate_code i.Geology_code
local timevarcontrols Precipitation Temperature

label var PA_relevant "Protected Areas by 2000"

set scheme white_tableau, perm


*_________________________________________________________________________________________________*
*
*				    		///// ROBUSTNESS CHECKS \\\\\
*_________________________________________________________________________________________________*


if 0{
*____________________________ ABSOLUTE FOREST CHANGE OUTCOME ______________________________________*

** DID with Cell FE Binary
xtreg Net_Recover post treat_post `timevarcontrols', fe i(cellID) cluster(cd_mun)
est store all
foreach v in Bahia EspiritoSanto MinasGerais Parana RioDeJaneiro SaoPaulo {
	quietly: xtreg Net_Recover post treat_post `timevarcontrols' if state=="`v'", fe i(cellID) cluster(cd_mun)

	est store `v', title(`v')
}
	
	*LaTeX Output Tables
	esttab  all Bahia EspiritoSanto MinasGerais Parana RioDeJaneiro SaoPaulo  using "$tables\RobustAbsForestBinary.tex", replace label starlevels(* 0.10 ** 0.05 *** 0.01)  se nonotes ///
	keep(post treat_post ) ///
	order(treat_post post ) ///
	varwidth(3)  style(tex) mtitles("Full" "Bahia" "Esp Santo" "Parana" "Min Gerais" "Rio de Jan" "Sao Paulo" ) b(%12.3f) se(%12.3f)
	
	
** DID with Cell FE Continuous
xtreg Net_Recover post post_treat_percent `timevarcontrols', fe i(cellID) cluster(cd_mun)
est store all
foreach v in Bahia EspiritoSanto MinasGerais Parana RioDeJaneiro SaoPaulo {
	quietly: xtreg Net_Recover post post_treat_percent `timevarcontrols' if state=="`v'", fe i(cellID) cluster(cd_mun)

	est store `v', title(`v')
}
	
	*LaTeX Output Tables
	esttab  all Bahia EspiritoSanto MinasGerais Parana RioDeJaneiro SaoPaulo  using "$tables\RobustAbsForestProp.tex", replace label starlevels(* 0.10 ** 0.05 *** 0.01)  se nonotes ///
	keep(post post_treat_percent ) ///
	order(post_treat_percent post ) ///
	varwidth(3)  style(tex) mtitles("Full" "Bahia" "Esp Santo" "Parana" "Min Gerais" "Rio de Jan" "Sao Paulo" ) b(%12.3f) se(%12.3f)
		
	
}

if 0{
	
*_________________________________ OTHER ROBUSTNESS  ____________________________________________*

g treatsq = treat_percent^2
g post_treat_percent_SQ = treatsq*post
la var post_treat_percent_SQ "Prop treated$^2$ x post" 
g post_percent_NF = (COV_NONFOREST/COV_Total)*post*100
la var post_percent_NF "\% non-forest x post"
g treat_percent_percent_NF = (COV_NONFOREST/COV_Total)*100* treat_percent
la var treat_percent_percent_NF "\% non-forest x treat"
g post_treat_percent_percent_NF = treat_percent_percent_NF * post
la var post_treat_percent_percent_NF "\% non-forest x treat x post"
g NetOverFarm = Net_Recover/COV_3Farming

*Quadratic
xtreg NetOverNF post post_treat_percent post_treat_percent_SQ `timevarcontrols', fe i(cellID) cluster(cd_mun)
	est store quad
*Interaction baseline forest
xtreg NetOverNF post post_treat_percent post_percent_NF treat_percent_percent_NF post_treat_percent_percent_NF `timevarcontrols', fe i(cellID) cluster(cd_mun)
	est store interact
*Over Farm Outcome 
xtreg NetOverFarm post post_treat_percent  `timevarcontrols', fe i(cellID) cluster(cd_mun)
	est store farm
	
	*LaTeX Output Table
	esttab  quad  interact farm using "$tables\OtherRobustness.tex", label replace  starlevels(* 0.10 ** 0.05 *** 0.01)  se nonotes ///
	keep(post_treat_percent post_treat_percent_SQ post_percent_NF post_treat_percent_percent_NF post)  order(post_treat_percent post_treat_percent_SQ post)  ///
	varwidth(3)  style(tex) mtitles("Quadratic" "Base NF" "Over farm") b(%12.3f) se(%12.3f) 
}


if 0{
*_________________________________ CEM Estimations ____________________________________________*	
use "$data\CEM_k2k_MASTER_MATCHED_R2.dta", clear
rename cell_uniqueID cellID

*Define Interactions
global interactions slope_treat slope_post slope_treatpost elevation_treat elevation_post elevation_treatpost Nearcitydist_treat Nearcitydist_post NearCitydist_treatpost

global pa_interactions PA_relevant PA_treat PA_post PA_treatpost
label var PA_relevant "Protected Areas by 2000"

**CEM (Table 3) MAIN SPECIFICATIONS (Period-Level) - All States
*(1) DID 
quietly: reg NetOverNF treat post treat_post elevation_km slope_100deg NearCity_dist_100km NearCity_population i.LandSurface_code i.Bioclimate_code i.Geology_code Precipitation Temperature  , cluster(cd_mun)
estimates store col1
estadd local Mun_FE "No"
estadd local Cell_FE "No"
*(2) DID + Municipality FE
quietly: xtreg NetOverNF treat post treat_post elevation_km slope_100deg NearCity_dist_100km NearCity_population i.LandSurface_code i.Bioclimate_code i.Geology_code Precipitation Temperature  , fe i(cd_mun) cluster(cd_mun)
estimates store col2
estadd local Mun_FE "Yes"
estadd local Cell_FE "No"
*(3) DID + Cell FE
xtset post cellID
quietly: xtreg NetOverNF post treat_post Precipitation Temperature  , fe i(cellID) cluster(cd_mun)
estimates store col3
estadd local Mun_FE "No"
estadd local Cell_FE "Yes"

*Show Table 
estout col1 col2 col3, cells(b(star fmt(6)) ) legend label keep(treat post treat_post elevation_km slope_100deg NearCity_dist_100km NearCity_population Precipitation Temperature  ) order(treat_post treat post elevation_km slope_100deg NearCity_dist_100km NearCity_population Precipitation Temperature  ) 

*LaTeX Output Table
esttab  col1 col2 col3  using "$tables\CEM_Table3.tex", label replace  starlevels(* 0.10 ** 0.05 *** 0.01)  se nonotes ///
keep(treat_post post treat  ) ///
order(treat_post post treat  ) ///
varwidth(3)  style(tex) mtitles("" "" "" "" "" "" ) b(%12.3f) se(%12.3f) scalar(Mun_FE Cell_FE)



**CEM (Table 4A) MAIN SPECIFICATION - DID with Cell FE by State
foreach v in Bahia EspiritoSanto MinasGerais Parana RioDeJaneiro SaoPaulo {
	quietly: xtreg NetOverNF post treat_post Precipitation Temperature if state=="`v'", fe i(cellID) cluster(cd_mun)

	est store `v', title(`v')
}
	*Table of Regressions
	estout Bahia EspiritoSanto MinasGerais Parana RioDeJaneiro SaoPaulo, cells(b(star fmt(3)) ) legend 
	
	*LaTeX Output Tables
	esttab  Bahia EspiritoSanto MinasGerais Parana RioDeJaneiro SaoPaulo  using "$tables\CEM_Table4A.tex", replace label starlevels(* 0.10 ** 0.05 *** 0.01)  se nonotes ///
	keep(post treat_post ) ///
	order(treat_post post ) ///
	varwidth(3)  style(tex) mtitles("Bahia" "Esp Santo" "Parana" "Min Gerais" "Rio de Jan" "Sao Paulo" ) b(%12.3f) se(%12.3f)
	
}

if 0{

*_______________________________ 1000m Spillover _____________________________________________*
*Main Estimations with 1000m Buffer 
use "$data\MASTER_MATCHED_R2_1000SPILL.dta", clear
rename cell_uniqueID cellID

*Define Interactions
global interactions slope_treat slope_post slope_treatpost elevation_treat elevation_post elevation_treatpost Nearcitydist_treat Nearcitydist_post NearCitydist_treatpost

global pa_interactions PA_relevant PA_treat PA_post PA_treatpost
label var PA_relevant "Protected Areas by 2000"

**Main Specifications
*(1) DID 
quietly: reg NetOverNF treat post treat_post elevation_km slope_100deg NearCity_dist_100km NearCity_population i.LandSurface_code i.Bioclimate_code i.Geology_code Precipitation Temperature  , cluster(cd_mun)
estimates store col1spill
estadd local Mun_FE "No"
estadd local Cell_FE "No"
*(2) DID + Municipality FE
quietly: xtreg NetOverNF treat post treat_post elevation_km slope_100deg NearCity_dist_100km NearCity_population i.LandSurface_code i.Bioclimate_code i.Geology_code Precipitation Temperature  , fe i(cd_mun) cluster(cd_mun)
estimates store col2spill
estadd local Mun_FE "Yes"
estadd local Cell_FE "No"
*(3) DID + Cell FE
xtset post cellID
quietly: xtreg NetOverNF post treat_post Precipitation Temperature  , fe i(cellID) cluster(cd_mun)
estimates store col3spill
estadd local Mun_FE "No"
estadd local Cell_FE "Yes"

*Show Table 
estout col1spill col2spill col3spill, cells(b(star fmt(6)) ) legend label keep(treat post treat_post elevation_km slope_100deg NearCity_dist_100km NearCity_population Precipitation Temperature  ) order(treat_post treat post elevation_km slope_100deg NearCity_dist_100km NearCity_population Precipitation Temperature  ) 

*LaTeX Output Table
esttab  col1spill col2spill col3spill  using "$tables\Spillover1000m.tex", label replace  starlevels(* 0.10 ** 0.05 *** 0.01)  se nonotes ///
keep(treat_post post treat  ) ///
order(treat_post post treat  ) ///
varwidth(3)  style(tex) mtitles("" "" "" "" "" "" ) b(%12.3f) se(%12.3f) scalar(Mun_FE Cell_FE)

}

if 1{

estimates clear

*_______________________________ No Spillover _____________________________________________*
*Main Estimations with 1000m Buffer 
use "$data\MASTER_MATCHED_R2_NOSPILL.dta", clear
rename cell_uniqueID cellID

*Define Interactions
global interactions slope_treat slope_post slope_treatpost elevation_treat elevation_post elevation_treatpost Nearcitydist_treat Nearcitydist_post NearCitydist_treatpost

global pa_interactions PA_relevant PA_treat PA_post PA_treatpost
label var PA_relevant "Protected Areas by 2000"

**Main Specifications
*(1) DID 
quietly: reg NetOverNF treat post treat_post elevation_km slope_100deg NearCity_dist_100km NearCity_population i.LandSurface_code i.Bioclimate_code i.Geology_code Precipitation Temperature  , cluster(cd_mun)
estimates store col1spill
estadd local Mun_FE "No"
estadd local Cell_FE "No"
*(2) DID + Municipality FE
quietly: xtreg NetOverNF treat post treat_post elevation_km slope_100deg NearCity_dist_100km NearCity_population i.LandSurface_code i.Bioclimate_code i.Geology_code Precipitation Temperature  , fe i(cd_mun) cluster(cd_mun)
estimates store col2spill
estadd local Mun_FE "Yes"
estadd local Cell_FE "No"
*(3) DID + Cell FE
xtset post cellID
quietly: xtreg NetOverNF post treat_post Precipitation Temperature  , fe i(cellID) cluster(cd_mun)
estimates store col3spill
estadd local Mun_FE "No"
estadd local Cell_FE "Yes"

*Show Table 
estout col1spill col2spill col3spill, cells(b(star fmt(6)) ) legend label keep(treat post treat_post elevation_km slope_100deg NearCity_dist_100km NearCity_population Precipitation Temperature  ) order(treat_post treat post elevation_km slope_100deg NearCity_dist_100km NearCity_population Precipitation Temperature  ) 

*LaTeX Output Table
esttab  col1spill col2spill col3spill  using "$tables\prespillover.tex", label replace  starlevels(* 0.10 ** 0.05 *** 0.01)  se nonotes ///
keep(treat_post post treat  ) ///
order(treat_post post treat  ) ///
varwidth(3)  style(tex) mtitles("" "" "" "" "" "" ) b(%12.3f) se(%12.3f) scalar(Mun_FE Cell_FE)

}


